package controllers

import (
	"encoding/json"
	beego "github.com/beego/beego/v2/server/web"
	"strconv"
	"syk/models"
	"time"
)

type MedicalController struct {
	beego.Controller
}

func (c *MedicalController) Get() {

}
func (c *MedicalController) Add() {
	name := c.GetString("name")
	sex := c.GetString("sex")
	tel := c.GetString("tel")
	doctor := c.GetString("doctor")
	time := c.GetString("time")
	num, _ := c.GetInt("num")
	zt := c.GetString("zt")
	text := c.GetString("text")
	if name == "" || sex == "" || tel == "" || doctor == "" || time == "" || num <= 0 || zt == "" || text == "" {
		res := JsonR{
			Code: 0,
			Msg:  "字段不能为空",
			Data: nil,
		}
		c.Data["json"] = res
		c.ServeJSON()
		return
	}
	medical := models.Medical{
		Name:   name,
		Sex:    sex,
		Tel:    tel,
		Doctor: doctor,
		Time:   time,
		Num:    num,
		Zt:     zt,
		Text:   text,
	}
	err := models.DB.Debug().Create(&medical).Error
	if err != nil {
		res := JsonR{
			Code: 0,
			Msg:  "添加失败",
			Data: nil,
		}
		c.Data["json"] = res
		c.ServeJSON()
		return
	} else {
		res := JsonR{
			Code: 200,
			Msg:  "添加成功",
			Data: medical,
		}
		c.Data["json"] = res
		c.ServeJSON()
	}
}
func (c *MedicalController) List() {
	pageData := make(map[string]interface{})
	query := models.DB.Model(&models.Medical{})
	page, _ := c.GetInt("page", 1)
	size, _ := c.GetInt("size", 10)
	name := c.GetString("name")
	key := "list-" + strconv.Itoa(page) + "-" + name
	if name != "" {
		query = query.Where("name like ?", "%"+name+"%")
	}
	val, err := models.Rdb.Get(key).Result()
	if err != nil {
		var count int64
		query.Count(&count)
		offset := (page - 1) * size
		var medical []models.Medical
		err = query.Offset(offset).Limit(size).Find(&medical).Error
		if err != nil {
			res := JsonR{
				Code: 0,
				Msg:  "查询失败",
				Data: nil,
			}
			c.Data["json"] = res
			c.ServeJSON()
		} else {
			pageData["count"] = count
			pageData["medical"] = medical
			jsonByte, _ := json.Marshal(&pageData)
			value := string(jsonByte)
			models.Rdb.Set(key, value, time.Minute*3)
			res := JsonR{
				Code: 200,
				Msg:  "查询成功",
				Data: pageData,
			}
			c.Data["json"] = res
			c.ServeJSON()
		}
	} else {
		json.Unmarshal([]byte(val), &pageData)
		res := JsonR{
			Code: 200,
			Msg:  "查询成功-redis",
			Data: pageData,
		}
		c.Data["json"] = res
		c.ServeJSON()
	}

}
func (c *MedicalController) Info() {
	id, _ := c.GetInt("id")
	key := "info-" + strconv.Itoa(id)
	val, err := models.Rdb.Get(key).Result()
	var medical models.Medical
	RanKKey := "rank-"
	if err != nil {
		err = models.DB.Debug().Where("id=?", id).First(&medical).Error
		if err != nil {
			res := JsonR{
				Code: 0,
				Msg:  "详情查询失败",
				Data: nil,
			}
			c.Data["json"] = res
			c.ServeJSON()
		} else {
			models.Rdb.ZIncrBy(RanKKey, 1, medical.Name)
			jsonByte, _ := json.Marshal(&medical)
			value := string(jsonByte)
			models.Rdb.Set(key, value, time.Minute*3)
			res := JsonR{
				Code: 200,
				Msg:  "详情查询成功",
				Data: medical,
			}
			c.Data["json"] = res
			c.ServeJSON()
		}
	} else {
		json.Unmarshal([]byte(val), &medical)
		models.Rdb.ZIncrBy(RanKKey, 1, medical.Name)
		res := JsonR{
			Code: 200,
			Msg:  "详情查询成功-redis",
			Data: medical,
		}
		c.Data["json"] = res
		c.ServeJSON()
	}

}
func (c *MedicalController) UpZt() {
	id, _ := c.GetInt("id")
	zt := c.GetString("zt")
	medical := models.Medical{
		Zt: zt,
	}
	err := models.DB.Where("id=?", id).Updates(&medical).Error
	if err != nil {
		res := JsonR{
			Code: 0,
			Msg:  "状态修改失败",
			Data: nil,
		}
		c.Data["json"] = res
		c.ServeJSON()
	} else {
		res := JsonR{
			Code: 200,
			Msg:  "状态修改成功",
			Data: medical,
		}
		c.Data["json"] = res
		c.ServeJSON()
	}
}
func (c *MedicalController) Rank() {
	key := "rank-"
	ranks, err := models.Rdb.ZRevRangeWithScores(key, 0, 6).Result()
	if err != nil {
		res := JsonR{
			Code: 0,
			Msg:  "排名失败",
			Data: nil,
		}
		c.Data["json"] = res
		c.ServeJSON()
	} else {
		res := JsonR{
			Code: 200,
			Msg:  "排名成功",
			Data: ranks,
		}
		c.Data["json"] = res
		c.ServeJSON()
	}
}
